package com.luo.shop.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.luo.shop.dao.CategoryDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.pojo.Category;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.Page;
import com.luo.shop.pojo.Category;
import com.luo.shop.service.admin.CategoryAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
@Service
public class CategoryadminServiceImpl implements CategoryAdminService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private GoodsDao goodsDao;
    
    public List<Category> selectAll() {
        List<Category> categories = categoryDao.selectAll();
        for (Category category : categories) {
            category.setParentCategory(categoryDao.selectByPrimaryKey(category.getPid()));
            Category isSon = new Category();
            isSon.setPid(category.getId());
            if (CollectionUtils.isEmpty(categoryDao.select(isSon))){
                category.setIsSon(false);
            }else{
                category.setIsSon(true);
            }
            Goods goods = new Goods();
            goods.setCid(category.getId());
            if (CollectionUtils.isEmpty(goodsDao.select(goods))){
                category.setIsGoods(false);
            }else{
                category.setIsGoods(true);
            }
        }
        return categories;
    }

    public Integer delete(Long id) {
        return categoryDao.deleteByPrimaryKey(id);
    }

    public Integer update(Category category) {
        return categoryDao.updateByPrimaryKeySelective(category);
    }

    public Integer save(Category category) {
        category.setCreatetime(new Date());
        return categoryDao.insertSelective(category);
    }
}
